package com.whoiszxl.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.whoiszxl.core.base.BaseController;
import com.whoiszxl.core.entity.ResponseResult;
import com.whoiszxl.cqrs.query.FmsFileQuery;
import com.whoiszxl.entity.FmsFile;
import com.whoiszxl.service.IFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author whoiszxl
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/file")
@Tag(name = "文件上传相关接口")
@RequiredArgsConstructor
public class FileController extends BaseController<IFileService, FmsFile, FmsFile, FmsFileQuery, FmsFile, FmsFile> {

    private final IFileService fileService;

    @SaCheckLogin
    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "上传文件")
    public ResponseResult<String> upload(
            @RequestParam(value = "file", required = true) MultipartFile file,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "bizId", required = false) String bizId,
            @RequestParam(value = "bizType", required = false) Integer bizType
            ) {

        if(file.isEmpty()) {
            return ResponseResult.buildError("文件内容为空");
        }

        String url = fileService.upload(id, bizId, bizType, file);
        return ResponseResult.buildSuccess(url);
    }

    @SaCheckLogin
    @DeleteMapping("/delete")
    @Operation(summary = "删除文件", description = "删除文件")
    public ResponseResult<Boolean> delete(@RequestParam(value = "ids[]") Long[] ids) {
        fileService.delete(ids);
        return ResponseResult.buildSuccess();
    }
    
}

